import { expect } from '@jest/globals';
import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import { addOhPackageDps } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
  modulesOption: {
    [ModuleTemplateType.SharedLibraryWithC]: 1,
  },
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};

const testFun: (module?: string) => TestCaseFun = (module?: string) => async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();
  await addOhPackageDps(project.getModule('entry').moduleRootPath(), {
    shareNativeCLibrary: '../shareNativeCLibrary',
  });
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap',
    ...(module ? { module: `${module}@default` } : {}),
  });
  const result = await runHvigorAsync(projectRootPath, commands);
  record.addStep('执行hap编译构建', result);
  expect(result.stdout).toContain(':shareNativeCLibrary:default@ProcessLibs');
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
};
TestCaseBuilder.builder()
  .caseName('OPEN_LAB_AIDE_ZOS_BUILD_API12_STAGE_WITH_HSP_DEPENDENCY_CHECK')
  .description('检查DependencyManager是否加载hsp依赖')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun('entry'))
  .load();
